﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

/* 说  明：加解密功能
 * 创  建：2024.01.02
 * 创建人：XSF
 * 更新说明：(序号，修改人，修改内容)
 * 
 */

namespace ShengYueWorkShop.BLL.PublicHelper
{
    /// <summary>
    /// 加解密功能
    /// </summary>
    public class Encryption
    {
        /// <summary>
        /// 秘钥
        /// </summary>
        private static string _frmKey = "s$D3_xaF";
        /// <summary>
        /// 进行DES加密。
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串。</param>
        /// <returns>以Base64格式返回的加密字符串。</returns>
        public static string Encrypt(string pToEncrypt)
        {
            try
            {
                if (string.Empty.Equals(pToEncrypt))
                    return string.Empty;
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                    des.Key = ASCIIEncoding.ASCII.GetBytes(_frmKey);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(_frmKey);
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Convert.ToBase64String(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }
            catch (SystemException)
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 进行DES解密。
        /// </summary>
        /// <param name="pToDecrypt">要解密的以Base64</param>
        /// <returns>已解密的字符串。</returns>
        public static string Decrypt(string pToDecrypt)
        {
            try
            {
                if (string.Empty.Equals(pToDecrypt))
                    return string.Empty;
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    des.Key = ASCIIEncoding.ASCII.GetBytes(_frmKey);
                    des.IV = ASCIIEncoding.ASCII.GetBytes(_frmKey);
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Encoding.UTF8.GetString(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }
            catch (SystemException)
            {
                return string.Empty;
            }
        }
    }
}
